from flask import Flask, request, jsonify
from flask_cors import CORS
# 使用缓存
# from functools import lru_cache

app = Flask(__name__)
# 支持所有跨域
CORS(app)
from views.hero import (
    getRelatedMessageByHeroName,
    getAllHeroMessage,
    getHeroDataByKeyword
)
from views.KGQA import (
    getQuestionAnswer
)
from utils.error import (
    ERROR_INVALID_PARAMS
)
from utils.wrapData import (
    wrapFailure
)
from views.region import (
    getAllRegionNodesData,
    getRegionNodeRelationshipsData
)
# 英雄相关接口

# 根据名称搜索英雄
@app.route('/searchHeroByName', methods=['POST'])
def searchHeroByName():
    params = request.get_json()
    if params is not None and 'heroName' in params:
        heroName = params['heroName']
        return jsonify(getRelatedMessageByHeroName(heroName))
    else:
        return jsonify(wrapFailure(**ERROR_INVALID_PARAMS))

# 获取所有英雄
@app.route('/getAllHeroData', methods=['GET'])
def getAllHeroData():
    return jsonify(getAllHeroMessage())

# 获取推荐英雄
@app.route('/getRecommendHeroData', methods=['GET'])
def getRecommendHeroData():
    return jsonify(getAllHeroMessage(6))

# 根据关键字搜索英雄
@app.route('/searchHeroByKeyword', methods=['POST'])
def searchHeroByKeyword():
    # 获取 JSON 数据
    params = request.get_json()
    if params is not None and 'keyword' in params:
        keyword = params['keyword']
        return jsonify(getHeroDataByKeyword(keyword))
    else:
        return jsonify(wrapFailure(**ERROR_INVALID_PARAMS))

# 区域相关接口

# 获取所有区域
@app.route('/getAllRegionsData', methods=['GET'])
def getAllRegionsData():
    return jsonify(getAllRegionNodesData())

# 根据名称获取某个区域节点的相关内容
@app.route('/getRegionDataByName', methods=['POST'])
def getRegionDataByName():
    # 获取 JSON 数据
    params = request.get_json()
    if params is not None and 'regionName' in params:
        regionName = params['regionName']
        return jsonify(getRegionNodeRelationshipsData(regionName))
    else:
        return jsonify(wrapFailure(**ERROR_INVALID_PARAMS))

# 知识问答接口
@app.route('/KGQA', methods=['POST'])
def knowledgeGraphQuestionAnswer():
    # 获取 JSON 数据
    params = request.get_json()
    if params is not None and 'sentence' in params:
        sentence = params['sentence']
        return jsonify(getQuestionAnswer(sentence))
    else:
        return jsonify(wrapFailure(**ERROR_INVALID_PARAMS))



if __name__ == '__main__':
    app.debug=True
    app.run(port=5004)